<?php

namespace App\Http\Controllers;
use Elasticsearch;
use App\Mongodb;
use App\shop_coupon;
use App\shop_lin_baby;
use App\shop_photo;
use App\shop_type;
use Illuminate\Http\Request;
use zgldh\QiniuStorage\QiniuStorage;
use Elasticsearch\ClientBuilder;
use Illuminate\Support\Facades\Redis;

class UserController extends Controller
{
    //查询店铺轮播图接口
  public function shop_photo(Request $request){
      //店铺id
      $shop_id=$request->shop_id;
      $data=shop_photo::all();
//      $data=shop_photo::where('shop_id',$shop_id)->get()->toArray();
       return response()->json($data) ;
  }
    //优惠券接口
    public function shop_coupon(Request $request){
        $shop_id=$request->shop_id;
        $data=shop_coupon::where('shop_id',$shop_id)->get()->toArray();
        return response()->json($data) ;

    }
    //店铺分类接口
    public function shop_type(Request $request){
        $shop_id=$request->shop_id;
        $data=shop_type::where('shop_id',$shop_id)->get()->toArray();
        return response()->json($data) ;
    }
    //店铺商品展示
    public function shop_show(Request $request){
        $shop_id=$request->shop_id;
        $data=shop_lin_baby::where('shop_id',$shop_id)->get()->toArray();
        return response()->json($data) ;
    }
    //商品升序降序展示
    public function shop_price_shop(Request $request){
        $shop_id=$request->shop_id;
        $status=$request->status;
        $status==1?$status='desc':$status='asc';
        $data=shop_lin_baby::where('shop_id',$shop_id)
            ->orderBy('s_baby_price',$status )
            ->get()->toArray();
        return $data;
    }
    //elasticsearc索引（数据表）
    public function elasticsearch(){
        //指定分词
        $client = Elasticsearch\ClientBuilder::create()->build();
        $params = [
            'index' => 'shopxm',
            'body' => [
                'settings' => [
                    'number_of_shards' => 3,
                    'number_of_replicas' => 2
                ],
                'mappings' => [

                    '_source' => [
                        'enabled' => true
                    ],
                    'properties' => [
                        's_baby_id' => [
                            'type' => 'integer'
                        ],
                        's_baby_name' => [
                            'type' => 'text',
                            "analyzer" => "ik_max_word",
                            "search_analyzer" => "ik_max_word"
                        ],
                        's_baby_price' => [
                            'type' => 'integer'
                        ],
                        's_baby_photo' => [
                            'type' => 'text'
                        ],
                        'shop_id' => [
                            'type' => 'integer'
                        ]
                    ]

                ]
            ]
        ];

        // Create the index with mappings and settings now
        $response = $client->indices()->create($params);
        print_r($response);
    }

    //删除索引
    public function elasticsearchDelete(){
        $client = Elasticsearch\ClientBuilder::create()->build();
        $params = ['index' => 'shopxm'];
        $response = $client->indices()->delete($params);
        print_r($response);

    }



  //格式化索引后添加数据
    public function elasticsearchAdd(){
        $data=shop_lin_baby::all()->toArray();

   foreach($data as $k=>$v){
        $client = ClientBuilder::create()->build();
        $params = [
            'index' => 'shopxm',
            'type' => '_doc',
            //'id' => '1',
            'body' => [
                's_baby_id' => $v['s_baby_id'],
                's_baby_name' =>$v['s_baby_name'],
                's_baby_price'=>$v['s_baby_price'],
                's_baby_photo'=>$v['s_baby_photo'],
                'shop_id'=>$v['shop_id']
            ]
        ];
        $response = $client->index($params);
        print_r($response);
    }
    }

    //添加数据
    public function elasticsearchAddone(Request $request){
        $data=shop_lin_baby::all()->toArray();
            $client = ClientBuilder::create()->build();
            $params = [
                'index' => 'shopxm',
                'type' => '_doc',
                'body' => [
                    's_baby_id' => $request['s_baby_id'],
                    's_baby_name' =>$request['s_baby_name'],
                    's_baby_price'=>$request['s_baby_price'],
                    's_baby_photo'=>$request['s_baby_photo'],
                    'shop_id'=>$request['shop_id']
                ]
            ];
            $response = $client->index($params);
            return response()->json($response);

    }

    //价格排序搜索数据
    public function search(Request $request){
        $search_name=$request->search_name;
        $status=$request->status;
        if($status==1){
            $status='asc';
        }else{
            $status='desc';
        }
        $client = Elasticsearch\ClientBuilder::create()->build();
        $params = [
            'index' => 'shopxm',
            //'type' => '_doc',
            'body' => [
                'query' => [
                    'match' => [
                        's_baby_name' => $search_name
                    ],

                ],
                'highlight' => [
                    'pre_tags' => ["<em>"],
                    'post_tags' => ["</em>"],
                    'fields' => [
                        "first_name" => new \stdClass()
                    ]


                ],
                "sort"=>[
                    "s_baby_price"=>[
                        "order"=>$status
                    ]
                ]
            ]
        ];
       $str=[];
        $results = $client->search($params);
        $data= $results['hits']['hits'];
        foreach($data as $k=>$v){
            if($v['_source']['shop_id']==1){
                $str[]=$v['_source'];
            }
        }
        return $str ;
    }
    //普通搜索
    public function search_pu(Request $request){
        $search_name=$request->search_name;
        $shop_id=$request->shop_id;
        $client = Elasticsearch\ClientBuilder::create()->build();
        $params = [
            'index' => 'shopxm',
            //'type' => '_doc',
            'body' => [
                'query' => [
                    'match' => [
                        's_baby_name' => $search_name,
                    ],

                ],
                'highlight' => [
                    'pre_tags' => ["<em>"],
                    'post_tags' => ["</em>"],
                    'fields' => [
                        "first_name" => new \stdClass()
                    ]


                ]
            ]
        ];
        $results = $client->search($params);

        $data= $results['hits']['hits'];
        $str=[];
       foreach($data as $k=>$v){
           if($v['_source']['shop_id']==$shop_id){
               $str[]=$v['_source'];
           }
       }
//        print_r($str);die;
        return $str ;
    }
    //Redis 商品添加
    public function gaoliang(){
        $data=shop_lin_baby::all()->toArray();
        foreach($data as $k=>$v){
            $s_baby_id= Redis::incr('s_baby_id');
            $res1=Redis::hset('shop_id',$s_baby_id,$s_baby_id);
            $res=Redis::hmset('shop:s_baby_id'.$s_baby_id,'s_baby_id',$v['s_baby_id'],'s_baby_name',$v['s_baby_name'],'s_baby_price',$v['s_baby_price'],'s_baby_photo',$v['s_baby_photo'],'shop_id',$v['shop_id']);
        }

//            $data = Redis::hmset('user:user_id' . $userID . '', $userID, $userID, $user, $user, $pwd, $pwd);
    }
    //Redis 商品展示
    public function redisshow(Request $request){
        $shop_id=$request->shop_id;
        $res=Redis::hgetall('shop_id');
       foreach($res as $k=>$v){
           $data=Redis::hgetall('shop:s_baby_id'.$v);
           if($data['shop_id']==$shop_id){
               $result[]=$data;
           }

       }
       return $result;

    }



}
